package nadav.tasher.lightool.graphics.view;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.*;
import ohos.agp.components.element.Element;
import ohos.agp.utils.LayoutAlignment;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class ExpandingView extends DirectionalLayout {
    static final HiLogLabel TAG = new HiLogLabel(HiLog.LOG_APP, 0x00201, "chenyu");
    private StackLayout topHolder, bottomHolder;
    private boolean isOpened = false, isAnimating = false;
    private int duration = 500;

    public ExpandingView(Context context) {
        super(context);
        init();
    }
    public void setDuration(int duration) {
        this.duration = duration;
    }

    @Override
    public void setBackground(Element element) {
        super.setBackground(element);
        initLayout();
    }

    public void setPadding(int horizontal, int vertical) {
        setPadding(horizontal, vertical, horizontal, vertical);
        HiLog.debug(TAG, "setPadding()  horizontal:"+horizontal+"  vertical:"+vertical);
    }

    public int getVerticalPadding1() {
        return getPaddingBottom() + getPaddingTop();
    }

    public int getHorizontalPadding1() {
        return getPaddingLeft() + getPaddingRight();
    }

    private void init(){
        HiLog.info(TAG, "ExpandingView init");
        topHolder = new StackLayout(getContext());
        topHolder.setLayoutConfig(new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_CONTENT));
        bottomHolder = new StackLayout(getContext());
        bottomHolder.setLayoutConfig(new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_CONTENT));
        setOrientation(VERTICAL);
        setAlignment(LayoutAlignment.TOP);
        setClickedListener(new ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (!isAnimating){
                    if (!isOpened){
                        animate(true);
                    }else {
                        animate(false);
                    }
                    isOpened = !isOpened;
                }
            }
        });
        addComponent(topHolder);
        addComponent(bottomHolder);
        initLayout();
    }

    public StackLayout getBottomHolder() {
        return bottomHolder;
    }

    public StackLayout getTopHolder() {
        return topHolder;
    }

    public void setBottom(Component c){
        HiLog.info(TAG, "ExpandingView setBottom");
        bottomHolder.removeAllComponents();
        bottomHolder.addComponent(c);
        initLayout();
    }

    public void setTop(Component v) {
        HiLog.info(TAG, "ExpandingView setTop");
        topHolder.removeAllComponents();
        topHolder.addComponent(v);
        initLayout();
    }

    private void initLayout(){
        HiLog.info(TAG, "ExpandingView initLayout");
        Utils.measure(topHolder);
        setLayoutConfig(new LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT,topHolder.getEstimatedHeight()+getVerticalPadding1()));
        HiLog.debug(TAG,"topHolder getEstimatedHeight: "+topHolder.getEstimatedHeight()+"  getVerticalPadding1"+getVerticalPadding1());
        setPadding(getHorizontalPadding1()/2,getVerticalPadding1()/2);
    }
    private void animate(final boolean open){
        Utils.measure(topHolder);
        Utils.measure(bottomHolder);

        int bottomHeight = bottomHolder.getEstimatedHeight();
        int topHeight = topHolder.getEstimatedHeight();
        int first, second;
        if (open) {
            first = topHeight + getVerticalPadding1();
            second = topHeight + bottomHeight + getVerticalPadding1();
        } else {
            first = topHeight + bottomHeight + getVerticalPadding1();
            second = topHeight + getVerticalPadding1();
        }
        AnimatorValue animator = new AnimatorValue();
        animator.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float v) {
                ComponentContainer.LayoutConfig layoutConfig = getLayoutConfig();
                layoutConfig.height = (int) ((v * (second - first)) + first);
                setLayoutConfig(layoutConfig);
            }
        });
        animator.setStateChangedListener(new Animator.StateChangedListener() {
            @Override
            public void onStart(Animator animator) {
                isAnimating = true;
            }

            @Override
            public void onStop(Animator animator) {

            }

            @Override
            public void onCancel(Animator animator) {

            }

            @Override
            public void onEnd(Animator animator) {
                isAnimating = false;
            }

            @Override
            public void onPause(Animator animator) {

            }

            @Override
            public void onResume(Animator animator) {
            }
        });
        animator.setDuration(duration);
        animator.start();
    }


}
